/**
 * Created by chentao on 2015/8/13.
 */

var userId="";

var Admin;
if (!Admin) {
    Admin = {}
}


Admin.showEnable = function(value,row,index){
    if(row.status==1){
        return ['<a href="javascript:void(0)" class="delete">删除</a>'+'&nbsp&nbsp&nbsp'+'<a href="javascript:void(0)" ' +
        'class="resetPwd">重置密码</a>'+'&nbsp&nbsp&nbsp'+'<a href="javascript:void(0)" onclick="forbid(' + 2 + ','+row.id+')">禁用</a>'].join('');
    }else{
        return ['<a href="javascript:void(0)" class="delete">删除</a>'+'&nbsp&nbsp&nbsp'+'<a href="javascript:void(0)" ' +
        'class="resetPwd">重置密码</a>'+'&nbsp&nbsp&nbsp'+'<a href="javascript:void(0)" style="color:red" onclick="forbid(' + 1 + ','+row.id+')">已禁用</a>'].join('');
    }

}

/**
 * 修改用户状态
 * @param enable
 * @param id
 */
var mergeEnable = function (enable, id) {
    comment.post("/web/user/mergeEnable", {id: id, enable: enable}, null, function (data) {
        if (data.res.code == 40000) {
            comment.refresh();
        } else {
            hint("操作失败");
        }
    });
    return false;
}

/**
 * 点击添加用户按钮
 */
bindClick("#addAdmin", function () {
    showModel("addAdmin-div", function () {

    });
});

window.userEvents={
    'click .delete':function(e,value,row,index){
        comment.showOperationTips("确认要删除该用户吗？",function(){
            $.post($().AbsUrlParse('/web/user/deleteUserById'),{
                id: value
            }, function(data){
                if(data.state==true){
                    if(data.res.code==40000){
                        hint("删除用户成功");
                        var table = $("#table");
                        table.bootstrapTable('refresh');
                    }else{
                        hint(data.res.msg);
                    }
                }else{
                    hint(data.msg);
                }
            });
        });
    },
    //重置密码
    'click .resetPwd':function(e,value,row,index){
        showModel("resetPwd",function(){
            $("#resetPwdForm input[name=newPwd]").val("")
          　userId = value;
        });
    }
}


//点击修改密码的按钮
$("#resetPwdFormButton").click(function(){
    var newPwd = $("#resetPwdForm input[name=newPwd]").val()

    if(newPwd==""||newPwd.trim()==""){
        hint("密码不能为空！")
        return false;
    }

    $.post($().AbsUrlParse("/web/user/resetPwd"),{
        id:userId,
        newPwd:newPwd
    },function(data){
        if(data.state==true){
            if(data.res.code==40000){
                hint("修改用户密码成功");
               closeModel("resetPwd")
            }else{
                hint(data.res.msg);
            }
        }else{
            hint(data.msg);
        }
    });
})

//修改用户的状态
function forbid(status,id){
    $.post($().AbsUrlParse("/web/user/mergeEnable"),{
        enable:status,
        id:id
    },function(data){
        var table = $("#table");
        table.bootstrapTable('refresh');
    });
}





//*****************************************************************************************

//提交表单后回调
var saveAdmin = function (data) {
    var d = JSON.parse(data);
    console.log(d);
}



Admin.editAdminShow = function(row,formid){
    setRowToFrm(formid, row);
    return false;
}


